﻿package{
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.display.DisplayObject;
	import flash.xml.*;
	import LoadStyle;
	public class spritter extends MovieClip{
		private var mainSprite:Sprite;//中间的主容器
		//private var topTextArea:TextArea;
		//private var buttomTextArea:TextArea;
		private var leftButton:Sprite;//左边向上按钮的容器
		private var rightButton:Sprite;//右边向下按钮的容器
		private var words:Array;//练习的词语
		private var selectedCharacterFrame:int;
		private var count:int;//计数器，计数器超时则退出
		private var cwps:Array;
		private var flag:Boolean;
		private var writtenFlag:Boolean;
		private var characterInput:TextField;
		private var loadStyle:LoadStyle;
		private var style:XMLList;
		private var textOfPinyin:TextField;
		private var textOfMeaning:TextField;
		private var tfOfMeaning:TextFormat;
		private var tfOfPinyin:TextFormat;
		private var cfCoordinates:Array;
		private var temArrayFlag:Array=[0,0,0,0];
		private var tempArray:Array=["ba4","ba4","hui2","guo2"];
		private var tempArray2:Array=["wholly shift!","你爸回国了！"];
		private var textOfPinyinFlag:Boolean;
		public function spritter(){
				this.loadStyle=new LoadStyle();
				this.style=new XMLList();
				this.cwps=new Array();
				this.mainSprite=new Sprite();
				this.leftButton=new Sprite();
				this.rightButton=new Sprite();
				this.words=new Array();
				this.words.push("爸");
				this.words.push("爸");
				this.words.push("回");
				this.words.push("国");
				this.addChild(meaningPrompt);
				this.addChild(pinyinPrompt);					
				pinyinPrompt.visible=false;
				this.textOfPinyinFlag=false;
				meaningPrompt.visible=false;
				pinyinPrompt.addEventListener(MouseEvent.CLICK,showPinyin);
				meaningPrompt.addEventListener(MouseEvent.CLICK,showMeaning);
				addChild(okk);
				 stage.scaleMode = "noScale";
				okk.addEventListener(MouseEvent.CLICK,submit);
				this.selectedCharacterFrame=0;
				//mainSprite.x=480-this.words.length*175/2;
				//mainSprite.y=132;
				this.flag=false;
				this.writtenFlag=false;
				count=0;
				addChild(this.mainSprite);
				addChild(writtenisok);
				writtenisok.addEventListener(MouseEvent.CLICK,writtenIsDone);
				addChild(eraser);
				eraser.addEventListener(MouseEvent.CLICK,erase);
				addChild(shang);
				shang.alpha=0;
				this.shang.addEventListener(MouseEvent.MOUSE_OVER,ButtonMouseOver);
				this.shang.addEventListener(MouseEvent.MOUSE_OUT,ButtonMouseOut);
				this.shang.addEventListener(MouseEvent.CLICK,previousword);
				addChild(xia);
				this.xia.alpha=0;
				this.xia.addEventListener(MouseEvent.MOUSE_OVER,ButtonMouseOver);
				this.xia.addEventListener(MouseEvent.MOUSE_OUT,ButtonMouseOut);
				this.xia.addEventListener(MouseEvent.CLICK,nextword);
				this.setChildIndex(this.mainSprite,4);
				characterInput=new TextField();			//初始化输入框
				with(characterInput){
					type=TextFieldType.INPUT;
					text="";
					border=false;				
					x=415;
					y=500;				
					height=20;
					width=150;
				}
				addChild(characterInput);
				this.textOfPinyin=new TextField();
				with(textOfPinyin){
					text="";
					border=false;
					x=400;
					y=73;
					height=60;
					width=400;
					}
				this.textOfMeaning=new TextField();
				with(textOfMeaning){
					text="";
					border=false;
					x=400;
					y=73;
					height=60;
					width=400;
					}
				this.tfOfMeaning=new TextFormat();
				this.tfOfPinyin=new TextFormat();
				this.addChild(textOfPinyin);
				this.addChild(textOfMeaning);
				this.setChildIndex(textOfPinyin,5);
				this.setChildIndex(textOfMeaning,5);
				//loadData();
				
			}
			function submit(e:MouseEvent){
				if(this.characterInput.text!=""){
					this.words=new Array();
					this.words=(this.characterInput.text).split("");
					this.cwps=new Array();
					
					this.mainSprite=null;
					this.mainSprite=new Sprite();
					addChild(this.mainSprite);
					mainSprite.x=style.mainSprite.mid_lineX-this.words.length*style.midRect.width/2;
					mainSprite.y=style.mainSprite.y;
					this.flag=false;
					this.characterInput.text="";
					loadData();
					}
				}
			function showPinyin(e:MouseEvent){
				this.textOfPinyinFlag=true;
				this.textOfPinyin.text=this.tempArray.join("  ");
				
				}
			function showMeaning(e:MouseEvent){
				this.textOfMeaning.text=this.tempArray2[1];
				}
			function writtenIsDone(e:MouseEvent){
				showMidRect();
				}
			function showMidRect(){
				var xx=Number(style.midRect.width);
				var xx1=Number(style.characterFrame.width);
				var s;
				for(var j=0;j<this.words.length;j++){
							this.cfCoordinates[j][0].x=(xx+2)*j;
							this.cfCoordinates[j][0].y=style.midRect.y;
							this.cfCoordinates[j][1]=xx/xx1;
					s=(characterFrame)(this.mainSprite.getChildByName(j));
					s.removeEventListener(MouseEvent.CLICK,showRect);
					}
				
				if(!this.mainSprite.hasEventListener(Event.ENTER_FRAME)){
					this.mainSprite.addEventListener(Event.ENTER_FRAME,changeCF);
					}
				
				//writtenOk();
				for(var k=0;k<this.words.length;k++){
					s=(characterFrame)(this.mainSprite.getChildByName(k));
					
					if(s.done()==true){
						writtenFlag=true;
						continue;
						}
					else{
						writtenFlag=false;
						break;
						}
					}
				if(writtenFlag==true){
					
					}
				}
			function erase(e:MouseEvent){
				var s=(characterFrame)(this.mainSprite.getChildAt(this.words.length-1));
				
				s.eraser();
				}
			function ButtonMouseOver(e:MouseEvent){
					e.target.removeEventListener(Event.ENTER_FRAME,out);
					e.target.addEventListener(Event.ENTER_FRAME,over);
				}
			function over(e:Event){
					if(e.target.alpha<1){
						e.target.alpha+=0.05;
					}
				}
			function ButtonMouseOut(e:MouseEvent){
					e.target.removeEventListener(Event.ENTER_FRAME,over);
					e.target.addEventListener(Event.ENTER_FRAME,out);
				}
			function out(e:Event){
					if(e.target.alpha>0){
						e.target.alpha-=0.05;
					}
				}
			function previousword(e:MouseEvent){
					trace("previousword");
				}
			function nextword(e:MouseEvent){
					trace("nextword");
				}
			
			function loadData(){
					 //cwp=new Cwp("把");//根据输入的汉子生成通信类实例
					// cwp.cwpLoad();//调用通信功能
					
					//trace(this.words[0]);
					 for(var k=0;k<this.words.length;k++){
						 var c=new LoadCharacter(this.words[k]);
						 this.cwps.push(c);
						 this.cwps[k].cwpLoad();
						 
						 }
					 this.mainSprite.addEventListener(Event.ENTER_FRAME,loaded);//添加enterframe事件监听通信完成
					
					 
				}
			 function loaded(e:Event){
				
				
				this.count=this.count+1;
				if(this.count>=1000){//通信超时
					this.count=0;
					trace("通信超时！");
					this.mainSprite.removeEventListener(Event.ENTER_FRAME,loaded);
					
					}
				for(var k=0;k<this.words.length;k++){
					
					if(this.cwps[k].getFlag()!=true||this.loadStyle.getFlag()!=true){
						this.flag=false;
						break;
						}
					this.flag=true;
				
				}
				if(this.flag==true){//连接成功
					
					this.mainSprite.removeEventListener(Event.ENTER_FRAME,loaded);
					this.count=0;
					trace("数据载入成功");
					
					//mainSprite.x=this.style.elements("words")-this.words.length*175/2;
					init();
					
					}
			
				
				}
			function init(){
				if(this.words.length==1){
					this.style=loadStyle.getLoadedStyle().elements("words").(@totle==1);
				}
				else if(this.words.length==2){
					this.style=loadStyle.getLoadedStyle().elements("words").(@totle==2);
				}
				else if(this.words.length==3){
					this.style=loadStyle.getLoadedStyle().elements("words").(@totle==3);
					}
				else if (this.words.length==4){
					this.style=loadStyle.getLoadedStyle().elements("words").(@totle==4);
					}
				else{
					this.style=loadStyle.getLoadedStyle().elements("words").(@totle==4);
					}
					this.pinyinPrompt.visible=true;
					this.meaningPrompt.visible=true;
					this.meaningPrompt.width=style.meaningPrompt.width;
					this.meaningPrompt.height=style.meaningPrompt.height;					
					this.meaningPrompt.x=style.meaningPrompt.x;
					this.meaningPrompt.y=style.meaningPrompt.y;
					this.pinyinPrompt.width=style.pinyinPrompt.width;
					this.pinyinPrompt.height=style.pinyinPrompt.height;					
					this.pinyinPrompt.x=style.pinyinPrompt.x;
					this.pinyinPrompt.y=style.pinyinPrompt.y;
					this.textOfPinyin.x=Number(style.pinyinPrompt.x)+(Number(style.pinyinPrompt.width)-400)/2;
					this.textOfPinyin.y=Number(style.pinyinPrompt.y)+(Number(style.pinyinPrompt.height)-30)/2;
					this.textOfMeaning.x=Number(style.meaningPrompt.x)+(Number(style.meaningPrompt.width)-400)/2;
					this.textOfMeaning.y=Number(style.meaningPrompt.y)+(Number(style.meaningPrompt.height)-30)/2;
					tfOfMeaning.align="center";
					tfOfMeaning.bold=false;
					tfOfMeaning.color=style.meaningTextFormat.color;
					tfOfMeaning.size=style.meaningTextFormat.size;
					tfOfMeaning.font=style.meaningTextFormat.font;
					tfOfPinyin.align="center";
					tfOfPinyin.bold=false;
					tfOfPinyin.color=style.pinyinTextFormat.color;
					tfOfPinyin.size=style.pinyinTextFormat.size;
					tfOfPinyin.font=style.pinyinTextFormat.font;
					trace(textOfPinyin.x);
					this.textOfPinyin.defaultTextFormat=tfOfPinyin;
					this.textOfMeaning.defaultTextFormat=tfOfMeaning;
					mainSprite.x=this.style.mainSprite.mid_lineX-this.words.length*style.midRect.width/2;
					mainSprite.y=this.style.mainSprite.y;
					this.cfCoordinates=new Array();
					var singleWidth:Number=Number(style.smallRect.width);
					var cfWidth:Number=style.characterFrame.width;
					var cleft:Number=Number(style.characterFrame.cover_left);
					var cright:Number=Number(style.characterFrame.cover_right);
					var topText:String="";
					for(var i=0;i<this.words.length;i++){
						
						var character:ChineseCharacter=new ChineseCharacter(this.cwps[i].getStrokes(),this.cwps[i].getBeziers());
						var cf:characterFrame=new characterFrame(character,style);
						topText+=" __ ";
						cf.name=i;
						//cf.x=300;cf.y=100;
						this.mainSprite.addChild(cf);
						this.mainSprite.setChildIndex(cf,i);
						cf.addEventListener(MouseEvent.CLICK,showRect);
						var tmp=new Array();
						tmp.push(new Point(0,0));
						tmp.push(1);
						cfCoordinates.push(tmp);
						
						cfCoordinates[i][1]=1;
						if(i==0){
							
							cf.showAll(0,0);
							}
						else{
							cf.x=(singleWidth+4)*(i-1-cleft-cright)+cfWidth;
							cf.y=style.smallRect.y;
							cf.showRect(singleWidth/cfWidth,singleWidth/cfWidth);
							}
						}
						this.mainSprite.setChildIndex(this.mainSprite.getChildByName("0"),this.words.length-1);
						this.textOfPinyin.text=topText;
						//sort(0);
				}
			function showRect(e:MouseEvent){
			trace("spritter");
			sort((int)(e.currentTarget.name));
				/*var obj:Array=new Array();
				var objTarget:characterFrame;
				obj=this.mainSprite.getObjectsUnderPoint(new Point(mouseX,mouseY));
				var k:int=0;
				
				for(k=0;k<obj.length;k++){
					if(obj[k] is characterFrame){
						objTarget=obj[k];
						break;
						}
					}
				while (k<obj.length&&(obj[k] is characterFrame)){
					if(this.mainSprite.getChildIndex(obj[k])>this.mainSprite.getChildIndex(objTarget)){
						objTarget=obj[k];
						}
					k++;
					}
				//trace(objTarget.name);
				//trace(this.mainSprite.getObjectsUnderPoint(new Point(mouseX,mouseY)));
					if(objTarget is characterFrame){
						sort((int)(objTarget.name));
					}*/
			
				}
			function sort(i){
					var s;
					
					for(var k=0;k<this.words.length;k++){
						s=(characterFrame)(this.mainSprite.getChildByName(k));
						if(s.done()==true){
							writtenFlag=true;
							
							continue;
							}
						else{
							writtenFlag=false;
							break;
							}
						
						}
					s=(characterFrame)(this.mainSprite.getChildByName(i));
					if(writtenFlag==true){
						showMidRect();
						}
					else if(s.done()==true&&s.getStates()=="all"){
						
						while(s.done()==true){
							i++;
							if(i==this.words.length){
								i=0;
								}
							s=(characterFrame)(this.mainSprite.getChildByName(i));
							}
						sort(i);
						}
					else{
						
						var topText:String="";
						this.selectedCharacterFrame=i;
						var singleWidth:Number=Number(style.smallRect.width);
						var cfWidth:Number=style.characterFrame.width;
						var cleft:Number=Number(style.characterFrame.cover_left);
						var cright:Number=Number(style.characterFrame.cover_right);
					
						for(var j=0;j<i;j++){
							s=(characterFrame)(this.mainSprite.getChildByName(j));
						
							//s.x=(singleWidth+3)*j;
							//s.y=style.smallRect.y;
							//s.showRect(singleWidth/cfWidth,singleWidth/cfWidth);
							this.mainSprite.setChildIndex(s,j);
							this.cfCoordinates[j][0].x=(singleWidth+2)*j;
							this.cfCoordinates[j][0].y=style.smallRect.y;
							this.cfCoordinates[j][1]=singleWidth/cfWidth;
							if(!s.done()&&this.temArrayFlag[j]==0){
								s.init();
								topText+=" __ ";
								}
							else{
								topText+="  "+tempArray[j]+"  ";
								}
						}
						s=(characterFrame)(this.mainSprite.getChildByName(i));
						if(i==0){
							//s.showAll((singleWidth+3)*(i),0);
							this.cfCoordinates[i][0].x=(singleWidth+2)*(i);
							this.cfCoordinates[i][0].y=0;
							this.cfCoordinates[i][1]=1;
						}
						else if(i==(this.words.length-1)){
							this.cfCoordinates[i][0].x=(singleWidth+2)*(i-cleft-cright);
							this.cfCoordinates[i][0].y=0;
							this.cfCoordinates[i][1]=1;
							}
						else{
							//s.showAll((singleWidth+3)*(i-cleft),0);
							this.cfCoordinates[i][0].x=(singleWidth+2)*(i-cleft);
							this.cfCoordinates[i][0].y=0;
							this.cfCoordinates[i][1]=1;
						}
						if(s.done()==true||this.temArrayFlag[i]==1){
							topText+="  "+this.tempArray[i]+"  ";
						}
						else{
							topText+=" __ ";
							}
					//s.showHint();
					//trace(s.x);
					
						this.mainSprite.setChildIndex(s,this.words.length-1);
						for(j=i+1;j<this.words.length;j++){
						
							 s=(characterFrame)(this.mainSprite.getChildByName(j));
						
							//s.x=(singleWidth+4)*(j-1-cleft-cright)+cfWidth;
							//s.y=style.smallRect.y;
							//s.showRect(singleWidth/cfWidth,singleWidth/cfWidth);
							//trace(s.scaleX);
							this.mainSprite.setChildIndex(s,this.words.length-1-j);
							this.cfCoordinates[j][0].x=(singleWidth+2)*(j-1-cleft-cright)+cfWidth;
							this.cfCoordinates[j][0].y=style.smallRect.y;
							this.cfCoordinates[j][1]=singleWidth/cfWidth;
							if(!s.done()&&this.temArrayFlag[j]==0){
							s.init();
							topText+=" __ ";
							}
							else{
							topText+="  "+tempArray[j]+"  ";
							}
						}
						if(this.textOfPinyinFlag==true){
						
						}
						else{
							this.textOfPinyin.text=topText;
						}
					//trace(this.cfCoordinates);
						if(!this.mainSprite.hasEventListener(Event.ENTER_FRAME)){
							this.mainSprite.addEventListener(Event.ENTER_FRAME,changeCF);
						}
					}
				}
			function changeCF(e:Event){
				var cfWidth:Number=style.characterFrame.width;
				var i:int=0;
				for(var j=0;j<this.words.length;j++){
						
						var s=(characterFrame)(this.mainSprite.getChildByName(j));
						
						if(Math.abs(s.x-this.cfCoordinates[j][0].x)>1||Math.abs(s.y-this.cfCoordinates[j][0].y)>1||Math.abs(s.scaleX-this.cfCoordinates[j][1])>0.01){
							s.x-=(s.x-this.cfCoordinates[j][0].x)*style.characterFrame.changeSpeed.moveSpeedX;
							s.y-=(s.y-this.cfCoordinates[j][0].y)*style.characterFrame.changeSpeed.moveSpeedY;
							var scl=s.scaleX-(s.scaleX-this.cfCoordinates[j][1])*style.characterFrame.changeSpeed.scaleSpeed;
							s.showRect(scl,scl);
							
							//trace(s.y+"//"+this.cfCoordinates[j][0].y+"//"+(s.y-this.cfCoordinates[j][0].y)*style.characterFrame.changeSpeed.moveSpeedY);
							}
						else{
							
							s.x=this.cfCoordinates[j][0].x;
							s.y=this.cfCoordinates[j][0].y;
							//s.y=0;
							if(this.cfCoordinates[j][1]==1){
								s.showAll(this.cfCoordinates[j][0].x,0);
								this.mainSprite.removeEventListener(Event.ENTER_FRAME,changeCF);
								}
							else{
								s.showRect(this.cfCoordinates[j][1],this.cfCoordinates[j][1]);
								}
							
							}
						
						
							
						}
				
				}
		
		}
	
	
}